package com.spq.paramsverify.common;

import com.alibaba.fastjson.JSON;
import org.apache.tomcat.util.buf.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description 全局异常处理类,
 * @Author gongjianpeng
 * @Date 2022/1/19
 */
@ControllerAdvice //将当前类标识为异常处理的组件
public class GlobalExceptionHandler {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    //设置要匹配的异常
    @ExceptionHandler(value = ParamException.class)
//    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public MessageBody defaultErrorHandler(HttpServletRequest request, ParamException e) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.error("access fail 系统错误: url={}, req={}", request.getRequestURL(), JSON.toJSONString(parameterMap), e);
        MessageBody body = new MessageBody();
        body.setCode(e.getCode());
        body.setErrorInfo(e.getMessage());
        body.setData(e.getData());
        return body;
    }
}
